package com.liuym.student.controller;

import com.liuym.common.constants.GradeResponse;
import com.liuym.common.exception.StudentException;
import com.liuym.student.repository.model.bo.StudentScoreBO;
import com.liuym.student.repository.model.vo.StudentScoreVO;
import com.liuym.student.service.StudentScoreService;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Liuym
 * @date 2018/12/10 10:45
 * @describe：
 */
@RestController
@RequestMapping("/studentScore")
public class StudentScoreController {

    private static Logger logger = LoggerFactory.getLogger(StudentScoreController.class);

    @Autowired
    private StudentScoreService studentScoreService;

    @PostMapping("/addStudentScore")
    public GradeResponse addStudentScore(String userName, StudentScoreVO studentScoreVO) {
        StudentScoreBO studentScoreBO = BeanMapper.map(studentScoreVO, StudentScoreBO.class);
        boolean result = false;
        String studentScoreTableName = "t_student_score";
        try {
            result = studentScoreService.addStudentScore(studentScoreTableName, userName, studentScoreBO);
        } catch (Exception e) {
            if (e instanceof StudentException) {
                logger.warn("warn", e);
                return GradeResponse.fail(((StudentException) e).getCode(), ((StudentException) e).getMsg());
            }
            logger.error("error", e);
            return GradeResponse.internalServerError();
        }
        return GradeResponse.success(result);
    }

}
